using System;
using GetProductsCSharp.com.ebay.developer1;

namespace GetProductsCSharp
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			// create and set up the service
			eBayAPIInterfaceService svc = new eBayAPIInterfaceService();

			// note the querystring has the siteid, callname, version, appname parameters
			svc.Url = "https://api.ebay.com/wsapi?siteid=0&callname=GetProducts&version=501&appname=xxx";
			// set the security credentials 
		//	svc.RequesterCredentials = new CustomSecurityHeaderType();
            svc.RequesterCredentials = new CustomSecurityHeaderType();
            svc.RequesterCredentials.Credentials.DevId = "aa493a58-84f2-4d76-9b44-0c4f30658540";
            svc.RequesterCredentials.Credentials.AppId = "BIGInter-6497-4934-862a-0edb36511b90";
            svc.RequesterCredentials.Credentials.AuthCert = "61066c80-dfd3-4ed5-a0a5-35da99cc1127";
            svc.RequesterCredentials.eBayAuthToken = "AgAAAA**AQAAAA**aAAAAA**1paqSQ**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wAkYenDpOEqAidj6x9nY+seQ**JpkAAA**AAMAAA**nvhuxkEG6ycpK3ohDndUupRYXwinPYXl9L8B0/Z8o5CLfBVSYu/j2Ysj0q3f0+IxF7W1WNhTnF4SQKXnOuU+a91ss3k5wLPWD8MKeKpd5DDqCQ97fIm0Ow7gYX6Wd4zRnPF+nEKfkShXMrxGKHBGKIKGkQUN33PpVHzze8i1VwDHICPTop7rEPYoehOh9fZVEyj+4ikBIu12WjnybkRwVfR6wCKNenm6ZcxXah8eAZ02LPsAUAKyG4SuNgNoPaNepxvig+8zf7iaeA2c/kTf7yb3fmsQtxpxtR4snV15WqYyP8zRp8N/ISuyHxZO7FPkbw3YoE1GDA+rMDwJOnIiJr9fVFSW3sEnd6v8A7g9mlgC3/W1jmLVHr9Cb8BCUOn1IY5LddBHYVbkHuJ3lhnK4X6oDuvvhct8e2NspvLa6WZB1FUXnbaaIhvyiN1sQkf+vMtxG0NW1jxYnhRmoUXCO+/IzqurWfpTZHx5BMh1JrnabCnFE9lJSoNqjp0RFTjRSlSJ28wAwv45Gqgv6C7ep1zarEv+2mrU8y+n2tT1DJ5DN8lcd1un59VP0mi/gu9cc6pEnpveb/LOydO3v5Vmy6nL78IsL7QBNES+sR2XHKQ9GNT+xPk987nSvBd1oTb02V24tPgy/4EqEFhe47whmzpV8gWKIKuBvCh3ZSEKQGCGxtma5o5dlGyBNYkwLqdokh4MJ7W5Yet20Eu/Lqe6/+zom4yMgj4QEpvo8RlTKayKYWhd7DLG2q2LzLJLDMGD";

			// create the request object
			GetProductsRequestType Request = new GetProductsRequestType();

			// set the DetailLevel ... this call actually does not require a DetailLevel
			// but we will set one anyway for completeness
			//http://developer.ebay.com/DevZone/SOAP/docs/WebHelp/GetProductsCall-GetProducts_Best_Practices.html
			Request.DetailLevel = new DetailLevelCodeType[] {DetailLevelCodeType.ReturnAll};

			// set the error handling to be conservative
			Request.ErrorHandlingSpecified = true;
			Request.ErrorHandling = ErrorHandlingCodeType.FailOnError;

			// set the WarningLevel to High
			Request.WarningLevelSpecified = true;
			Request.WarningLevel = WarningLevelCodeType.High;

			// see Table 5-2 here for list of error language strings
			// http://developer.ebay.com/DevZone/SOAP/docs/WebHelp/InvokingWebServices-Standard_Data_for_All_Calls.html
			Request.ErrorLanguage = "en_US";

			// set the Version of the call
			Request.Version = "501";

			// there are a significant number of ways this call can be used
			// some of the options are commented out, and can be activated
			// by commenting and uncommenting lines as directed

			// the ProductSearch container has the fields that forms the search query
			ProductSearchType search = new ProductSearchType();
			
			// if we wanted to do a general query on one or more keywords
			// we would indicate only the QueryKeywords input
			// if you wish to do this, uncomment the next 3 code lines
			// and comment out the 7 code lines below that set ExternalProductID and IncludeItemArray
			//search.QueryKeywords = "harry potter";
			// let's look for only products that have real items available
			//search.AvailableItemsOnlySpecified = true;
			//search.AvailableItemsOnly = true;

			// if we wanted to search for a specific
			// ISBN or UPC we would not use QueryKeywords
			// we would use ExternalProductID
			// for this example, we will use the UPC value 085392133123
			// which is a Harry Potter and the Sorcerer's Stone DVD
			ExternalProductIDType UPC = new ExternalProductIDType();
			UPC.TypeSpecified = true;
			UPC.Type = ExternalProductCodeType.UPC;
			UPC.Value = "085392133123";
			search.ExternalProductID = UPC;

			// let's include real items in the response
			Request.IncludeItemArraySpecified = true;
			Request.IncludeItemArray = true;

			// if you wish to include review details
			// then uncomment the next 2 lines
			// keep in mind that this is not applicable with QueryKeywords
			//Request.IncludeReviewDetailsSpecified = true;
			//Request.IncludeReviewDetails = true;

			// Set ProductSearchID if you wish to provide your own unique
			// identifier to this specific ProductSearch container
			search.ProductSearchID = "UPCSearchWithItems";


			// create a response object for this call
			GetProductsResponseType Response = new GetProductsResponseType();

			// we are now ready to make the call
			try
			{
				Response = svc.GetProducts(Request);

				// if no errors occurred that did not trigger an exception
				// then the result of this call returns a single Product container
				// and an ItemArray, other variations on this call such as using
				// QueryKeywords may return an array of Products
				if (true == Response.AckSpecified && (AckCodeType.Success == Response.Ack || AckCodeType.Warning == Response.Ack))
				{
					// print out the details of this Product
					foreach (CatalogProductType Product in Response.Product)
					{
						if (null != Product.Title)
						{
							Console.WriteLine("The Title of this product is " + Product.Title);
						}
						if (null != Product.DetailsURL)
						{
							Console.WriteLine("The DetailsURL of this product is " + Product.DetailsURL);
						}
						if (null != Product.StockPhotoURL)
						{
							Console.WriteLine("The StockPhotoURL of this product is " + Product.StockPhotoURL);
						}
						if (true == Product.ItemCountSpecified)
						{
							Console.WriteLine("The item count for this product is " + Product.ItemCount.ToString());
						}
						if (null != Product.ExternalProductID)
						{
							if (true == Product.ExternalProductID.TypeSpecified)
							{
								Console.WriteLine("The ExternalProductID for this product is of type " + Product.ExternalProductID.Type.ToString());
							}
							if (null != Product.ExternalProductID.Value)
							{
								Console.WriteLine("The ExternalProductID value of this product is " + Product.ExternalProductID.Value);
							}
						}
						if (true == Product.ProductReferenceIDSpecified)
						{
							Console.WriteLine("The eBay ProductReferenceID of this product is " + Product.ProductReferenceID.ToString());
						}
					}
				}
			}
			catch (System.Web.Services.Protocols.SoapException ex)
			{
				Console.WriteLine(ex.Detail.OuterXml.ToString() + ".", "SOAP Error");
			}
			catch (System.Exception ex)
			{
				Console.WriteLine(ex.Message, "General Error");
			}
		}
	}
}
